package com.goldze.mvvmhabit.ui.news;

import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;

import com.goldze.mvvmhabit.bean.NewsDetailBean;
import com.goldze.mvvmhabit.data.DemoRepository;
import com.goldze.mvvmhabit.okgo.HTTPAPI;
import com.goldze.mvvmhabit.utils.GsonUtil;
import com.goldze.mvvmhabit.utils.SpUtils;
import com.lzy.okgo.callback.StringCallback;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.LogUtil;
import okhttp3.Call;
import okhttp3.Response;

public class NewsDetailViewModel extends BaseViewModel<DemoRepository> {
    //用户名的绑定
    public ObservableField<String> title = new ObservableField<>("");
    //密码的绑定
    public ObservableField<String> contect = new ObservableField<>("");
    //时间
    public ObservableField<String> time = new ObservableField<>("");

    public SingleLiveEvent<String> url = new SingleLiveEvent<>();//图片地址

    public NewsDetailViewModel(@NonNull Application application, DemoRepository repository) {
        super(application, repository);

    }

    /**
     * 获取新闻详情
     */
    public void getNewsDetail(String id) {
        String token = (String) SpUtils.get("module_token","");
        HTTPAPI.getInstance().getNewsDetail(token, id, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("新闻详情-->" + s);
                NewsDetailBean newsDetailBean = GsonUtil.INSTANCE.parse(s,NewsDetailBean.class);
                if (newsDetailBean.getCode() == 200) {
                    title.set(newsDetailBean.getResult().getTitle());
                    contect.set(newsDetailBean.getResult().getContent());
                    time.set(newsDetailBean.getResult().getCreateTime());
                    url.setValue(newsDetailBean.getResult().getPic());
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
